package com.example.bootasync.listener;

import cn.hutool.json.JSONObject;
import com.example.bootasync.entity.bo.OrderMsgBo;
import com.example.bootasync.entity.bo.ProdMsgBo;
import com.example.bootasync.service.IOrderService;
import com.example.bootasync.service.IProdService;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : JinMing Zhou
 * @description: 秒杀监听
 * @date : 2023/6/3 11:15
 */
@Component
public class SeckillListener {

    @Resource
    private IProdService prodService;

    @Resource
    private IOrderService orderService;

    /**
     * 更新库存监听
     * @param msg 消息内容为bo
     */
    @RabbitListener(queuesToDeclare = @Queue("update_prod"))
    public void updateProd(String msg){
        prodService.updateProd(new JSONObject(msg).toBean(ProdMsgBo.class));
    }

    /**
     * 存储订单监听
     * @param msg 消息内容为bo
     */
    @RabbitListener(queuesToDeclare = @Queue("create_order"))
    public void saveOrder(String msg){
        orderService.saveOrder(new JSONObject(msg).toBean(OrderMsgBo.class));
    }

}
